package com.example.shop_2212.xq

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.CommentEntityResult
import com.example.base.entity.GoodsDetailEntityResult
import com.example.base.entity.GoodsEntityResult
import com.example.base.entity.SaveEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class XqViewModel:BaseViewModel() {
    //返回值--详情
    val xqSuccess = MutableLiveData<GoodsDetailEntityResult>()
    val xqFail = MutableLiveData<String>()
    fun xq(id:Int){
        viewModelScope.launch {
            try {
                xqSuccess.value = apiServer.xq(id,"normal")
            }catch (e:Exception){
                xqFail.value = e.message
            }
        }
    }
    //返回值---优选商品推荐
    val yxSuccess = MutableLiveData<GoodsEntityResult>()
    val yxFail = MutableLiveData<String>()
    fun yx(){
        viewModelScope.launch {
            try {
                yxSuccess.value = apiServer.yx()
            }catch (e:Exception){
                yxFail.value = e.message
            }
        }
    }
    //返回值--保存购物车
    val saveSuccess = MutableLiveData<SaveEntityResult>()
    val saveFail = MutableLiveData<String>()
    fun save(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("cartNum",cartNum)
                map.put("productAttrUnique",productAttrUnique)
                map.put("productId",productId)
                saveSuccess.value = apiServer.save(mapToBody(map))
            }catch (e:Exception){
                saveFail.value = e.message
            }
        }
    }
    //返回值--评论
    val commentSuccess = MutableLiveData<CommentEntityResult>()
    val commentFail = MutableLiveData<String>()
    fun comment(id:Int,limit:Int,page:Int,type:String){
        viewModelScope.launch {
            try {
                commentSuccess.value = apiServer.comment(id, limit, page, type)
            }catch (e:Exception){
                commentFail.value = e.message
            }
        }
    }
}